package hc.j2me;

import java.io.UnsupportedEncodingException;

import javax.microedition.lcdui.Displayable;

import hc.core.ClientCmdExector;
import hc.core.ContextManager;
import hc.core.IConstant;
import hc.core.MsgBuilder;
import hc.core.util.HCURL;
import hc.core.util.HCURLUtil;
import hc.core.util.IHCURLAction;
import hc.core.util.LogManager;
import hc.j2me.load.Mlet;
import hc.j2me.ui.ConfigForm;
import hc.j2me.ui.HCCtrlGameCanvas;
import hc.j2me.ui.PNGGameCanvas;
import hc.j2me.ui.UIManager;

/**
 * 注意：
 * 本类不同于CanvasMenu和Form，它俩为响应内容中所出现的HCURL。
 * 而本类是要处理来源于服务器的HCURL
 *
 */
public class J2MEURLAction implements IHCURLAction {
	public final static J2MEURLAction defaultJ2MEURLActon = new J2MEURLAction();
	
	public boolean doBiz(HCURL hcurl) {

//		hc.core.L.V=hc.core.L.O?false:LogManager.log("goto " + hcurl.url);
		if(ClientCmdExector.process(hcurl)){
		}else{
			if(hcurl.protocal == HCURL.SCREEN_PROTOCAL){
				if(hcurl.elementID.equals(HCURL.REMOTE_HOME_SCREEN)){
					HCCtrlGameCanvas.enterRemoteScreen(hcurl.url);
				}else{
					Mlet.enterActionCanvas(hcurl);
				}
				return true;
			}else if(hcurl.protocal == HCURL.CMD_PROTOCAL){
				final String cmd = hcurl.elementID;
				if(cmd.equals(HCURL.DATA_CMD_EXIT)){
					//此行必须先，因为下行可能导致系统关闭，而产生不能发送出去的情形。
					//由于可能在多个Menu间跳转，所以通知服务器端，返回前置Menu
					ContextManager.getContextInstance().send(MsgBuilder.E_GOTO_URL, hcurl.url);

					//处理本地画面切换
					UIManager.exitOrReturn();
					
					return true;
				}else if(cmd.equals(HCURL.DATA_CMD_CONFIG)){
					//"cmd://localhost/config";
					ConfigForm.showConfigForm();
					return true;
				}else if(cmd.equals(HCURL.DATA_CMD_SendPara)){
					final String firstPara = hcurl.getParaAtIdx(0);
					String v;
					
					if(firstPara.equals(HCURL.DATA_PARA_CLASS)){
						v = hcurl.getValueofPara(HCURL.DATA_PARA_CLASS);
						String className = v;
						HCURL.setToContext(hcurl);
						
						try {
							Class.forName(className).newInstance();
						} catch (Throwable e) {
							e.printStackTrace();
						}
						
						return true;
					}else if(firstPara.equals(HCURL.DATA_PARA_TRANSURL)){
						v = hcurl.getValueofPara(HCURL.DATA_PARA_TRANSURL);
						String decodeurl;
						try {
							decodeurl = ClientCmdExector.decodeURL(v, IConstant.UTF_8);
							HCURLUtil.process(decodeurl, J2MEURLAction.defaultJ2MEURLActon);
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
						return true;
					}else if(firstPara.equals(HCURL.DATA_PARA_INPUT)){
						v = hcurl.getValueofPara(HCURL.DATA_PARA_INPUT);//如支持全部数据类型的all
						Displayable d = UIManager.getCurrDisplayable();
						if(d != null && d instanceof PNGGameCanvas){
							((PNGGameCanvas)d).showTextUI(null);
						}
						return true;
					}
				}
			}else if(hcurl.protocal == HCURL.FORM_PROTOCAL 
					|| hcurl.protocal == HCURL.MENU_PROTOCAL){
				ContextManager.getContextInstance().send(MsgBuilder.E_CANVAS_MAIN, hcurl.url);
				return true;
			}
			
			//用户选定的菜单项URL
			ContextManager.getContextInstance().send(MsgBuilder.E_GOTO_URL, hcurl.url);
		}
		return false;
	}
}
